/* 
 * File:   ModelListHandler.cpp
 * Author: Guilherme Monteiro
 * 
 */

#include "../headers/ModelListHandler.h"

/**
 * Constructor of ModelListHandler class.
 * 
 * @param strModelList: list of models separated by white space
 */
ModelListHandler::ModelListHandler(string strModelList) throw (InvalidModelException) {
    vector<string> str_models_vec(NUMBER_OF_MODELS, "");
    
    if(strModelList.empty()) {
        throw InvalidModelException();
    }
    
    try {
        stringstream str_stream(strModelList);
        str_stream >> str_models_vec[0] >> str_models_vec[1];
    }
    catch(exception &e) {
        throw InvalidModelException();
        cerr << "Invalid list of models. The correct format is 'modelx modely modelz', separeated by white space" << endl;
    }
    
    std::set<string> setOfStrModels;
    for( unsigned i = 0; i < str_models_vec.size(); ++i ) {
        lowerCase(str_models_vec[i]);
        setOfStrModels.insert( str_models_vec[i] );
    }
    
    GenericModel* m1 = new Model1();
    GenericModel* m2 = new Model2();
    set<string>::iterator itSet;
    for(itSet = setOfStrModels.begin(); itSet != setOfStrModels.end(); itSet++) {
        if(strcmp((*itSet).c_str(), "") == 0) {
            continue;
        }
        
        if( strcmp((*itSet).c_str(), m1->getModelName().c_str()) == 0 ) { 
            this->modelsVec.push_back(m1);
        }
        else if( strcmp((*itSet).c_str(), m2->getModelName().c_str()) == 0 ) {
            GenericModel* m2 = new Model2();
            this->modelsVec.push_back(m2);
        }
        else {
            throw InvalidModelException();
        }
    }
}

ModelListHandler::ModelListHandler(const ModelListHandler& orig) {
}

ModelListHandler::~ModelListHandler() {
}

/**
 * Create a list of models, ordered
 * and without repeating elements.
 * 
 * @return vector of generic models
 */
vector<GenericModel*> ModelListHandler::getModelList() {
    return this->modelsVec;
}

/**
 * Function to convert one string
 * to lowercase mode.
 * 
 * @param strToConvert
 */
void ModelListHandler::lowerCase(string& strToConvert) {
   for(unsigned int i=0; i < strToConvert.length(); i++)
   {
      strToConvert[i] = tolower(strToConvert[i]);
   }
}